package com.jumi.microservice.controller.inner;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jm.member.common.api.web.R;
import com.jumi.microservice.dto.reponse.FenXiaoReponse;
import com.jumi.microservice.dto.request.FenXiaoQueryRequest;
import com.jumi.microservice.entity.JmUser;
import com.jumi.microservice.entity.JmUserFenxiao;
import com.jumi.microservice.enums.CommonEnum;
import com.jumi.microservice.enums.FenXiaoStatus;
import com.jumi.microservice.enums.Level;
import com.jumi.microservice.service.*;
import com.jumi.microservice.util.WrapperUtil;
import io.swagger.annotations.Api;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

@Api(value = "内部调用--收益",tags = "内部调用--收益")
@DubboService
@RestController
@RequestMapping("/inner/fenxiao")
public class InnerFenXiaoController implements IFenXiaoService {

    @Autowired
    IJmUserFenxiaoService fenxiaoService;

    @Autowired
    IJmUserService userService;


    /**
     *
     * @param orderSn 订单编号
     * @param buyId 购买人id
     * @param gid  商品id
     * @return
     */
    @PostMapping("/getSumFenxiao")
    public List<FenXiaoReponse> getSumFenxiao(String orderSn,Integer buyId,Integer gid){
        JmUserFenxiao fenxiao=new JmUserFenxiao();
        fenxiao.setOrderSn(orderSn);
        fenxiao.setGid(gid);
        fenxiao.setIsDel(CommonEnum.否);
        fenxiao.setBuyerId(buyId);
        QueryWrapper wrapper = WrapperUtil.getWrapper(null, fenxiao);
        wrapper.ne("status", FenXiaoStatus.失效);
        wrapper.groupBy("type");
        List<FenXiaoReponse> list = fenxiaoService.getSumYongJinByColumn(wrapper);
        return list;
    }

    @PostMapping("/getFenxiao")
    public R<Page> getFenxiao(String orderSn,Integer gid){
        JmUserFenxiao fenxiao=new JmUserFenxiao();
        fenxiao.setOrderSn(orderSn);
        fenxiao.setGid(gid);
        fenxiao.setIsDel(CommonEnum.否);
        QueryWrapper wrapper = WrapperUtil.getWrapper(null, fenxiao);
        wrapper.ne("status", FenXiaoStatus.失效);
        Page page=fenxiaoService.getPage(new Page(1,100),wrapper);
        return R.ok(page);
    }


    @Override
    public BigDecimal getSumFenxiao(FenXiaoQueryRequest request) {
        String mobile = request.getMobile();
        JmUser userByMobile = userService.getUserByMobile(mobile);
        JmUserFenxiao fenxiao=new JmUserFenxiao();
        if(ObjectUtil.isNotEmpty(userByMobile)){
            fenxiao.setUid(userByMobile.getId());
        }
        fenxiao.setStatus(request.getStatus());
        fenxiao.setLevel(Level.getLevelByValue(request.getLevel()==null?0:request.getLevel()));
        fenxiao.setType(request.getType());
        fenxiao.setIsDel(CommonEnum.否);
        QueryWrapper wrapper = WrapperUtil.getWrapper(null, fenxiao);
        return fenxiaoService.getSumYongJin(wrapper);
    }

    @Override
    public  List<FenXiaoReponse> getSumFenXiaoByUids(List<Integer> uids) {
        JmUserFenxiao fenxiao=new JmUserFenxiao();
        fenxiao.setStatus(FenXiaoStatus.未结算);
        fenxiao.setIsDel(CommonEnum.否);
        QueryWrapper wrapper = WrapperUtil.getWrapper(null, fenxiao);
        wrapper.groupBy("uid");
        wrapper.in("uid",uids);
        List<FenXiaoReponse> list = fenxiaoService.getSumYongJinByColumn(wrapper);
        return list;
    }


}
